/**
 * 智慧社区管理系统 - 移动端在线支付API接口
 * @author Wu.Liang
 * @date 2025-01-21
 * @description 移动端工程位于smart-community-app文件夹，严格遵循架构总览中的项目结构规范
 */

import request from '@/api/request'

// 统一支付API
export const unifiedPaymentApi = {
  // 创建统一支付订单
  createUnifiedPayment: (data) => {
    return request.post('/api/payments/unified', data)
  },

  // 获取支付订单详情
  getPaymentOrderDetail: (orderId) => {
    return request.get(`/api/payments/orders/${orderId}`)
  },

  // 查询支付状态
  queryPaymentStatus: (orderId) => {
    return request.get(`/api/payments/orders/${orderId}/status`)
  },

  // 取消支付订单
  cancelPaymentOrder: (orderId) => {
    return request.post(`/api/payments/orders/${orderId}/cancel`)
  },

  // 退款申请
  refundPayment: (orderId, data) => {
    return request.post(`/api/payments/orders/${orderId}/refund`, data)
  }
}

// 支付渠道API
export const paymentChannelApi = {
  // 获取可用支付方式
  getAvailableMethods: (params) => {
    return request.get('/api/payments/channels/available', { params })
  },

  // 获取支付渠道配置
  getChannelConfig: (channelCode) => {
    return request.get(`/api/payments/channels/${channelCode}/config`)
  },

  // 更新支付渠道配置
  updateChannelConfig: (channelCode, data) => {
    return request.put(`/api/payments/channels/${channelCode}/config`, data)
  }
}

// 费用账单API
export const billApi = {
  // 分页查询账单列表
  getBillList: (params) => {
    return request.get('/api/bills', { params })
  },

  // 获取账单详情
  getBillDetail: (billId) => {
    return request.get(`/api/bills/${billId}`)
  },

  // 创建账单
  createBill: (data) => {
    return request.post('/api/bills', data)
  },

  // 更新账单
  updateBill: (billId, data) => {
    return request.put(`/api/bills/${billId}`, data)
  },

  // 删除账单
  deleteBill: (billId) => {
    return request.delete(`/api/bills/${billId}`)
  },

  // 获取待缴费账单
  getPendingBills: (params) => {
    return request.get('/api/bills/pending', { params })
  },

  // 获取账单统计
  getBillStatistics: (params) => {
    return request.get('/api/bills/statistics', { params })
  }
}

// 缴费记录API
export const paymentRecordApi = {
  // 分页查询缴费记录
  getPaymentRecordList: (params) => {
    return request.get('/api/payments/records', { params })
  },

  // 获取缴费记录详情
  getPaymentRecordDetail: (recordId) => {
    return request.get(`/api/payments/records/${recordId}`)
  },

  // 创建缴费记录
  createPaymentRecord: (data) => {
    return request.post('/api/payments/records', data)
  },

  // 获取缴费统计
  getPaymentStatistics: (params) => {
    return request.get('/api/payments/statistics', { params })
  }
}

// 发票管理API
export const invoiceApi = {
  // 分页查询发票列表
  getInvoiceList: (params) => {
    return request.get('/api/invoices', { params })
  },

  // 获取发票详情
  getInvoiceDetail: (invoiceId) => {
    return request.get(`/api/invoices/${invoiceId}`)
  },

  // 申请发票
  applyInvoice: (data) => {
    return request.post('/api/invoices', data)
  },

  // 更新发票
  updateInvoice: (invoiceId, data) => {
    return request.put(`/api/invoices/${invoiceId}`, data)
  },

  // 删除发票
  deleteInvoice: (invoiceId) => {
    return request.delete(`/api/invoices/${invoiceId}`)
  },

  // 下载发票
  downloadInvoice: (invoiceId) => {
    return request.get(`/api/invoices/${invoiceId}/download`)
  },

  // 获取发票统计
  getInvoiceStatistics: (params) => {
    return request.get('/api/invoices/statistics', { params })
  }
}

// 支付设置API
export const paymentSettingApi = {
  // 获取支付设置
  getPaymentSettings: () => {
    return request.get('/api/payments/settings')
  },

  // 更新支付设置
  updatePaymentSettings: (data) => {
    return request.put('/api/payments/settings', data)
  },

  // 获取自动缴费设置
  getAutoPaymentSettings: () => {
    return request.get('/api/payments/settings/auto-payment')
  },

  // 更新自动缴费设置
  updateAutoPaymentSettings: (data) => {
    return request.put('/api/payments/settings/auto-payment', data)
  },

  // 获取支付方式设置
  getPaymentMethodSettings: () => {
    return request.get('/api/payments/settings/methods')
  },

  // 更新支付方式设置
  updatePaymentMethodSettings: (data) => {
    return request.put('/api/payments/settings/methods', data)
  }
}

// 费用类型API
export const feeTypeApi = {
  // 获取费用类型列表
  getFeeTypeList: (params) => {
    return request.get('/api/fee-types', { params })
  },

  // 获取费用类型详情
  getFeeTypeDetail: (typeId) => {
    return request.get(`/api/fee-types/${typeId}`)
  },

  // 创建费用类型
  createFeeType: (data) => {
    return request.post('/api/fee-types', data)
  },

  // 更新费用类型
  updateFeeType: (typeId, data) => {
    return request.put(`/api/fee-types/${typeId}`, data)
  },

  // 删除费用类型
  deleteFeeType: (typeId) => {
    return request.delete(`/api/fee-types/${typeId}`)
  }
}

// 支付工具类
export const paymentUtils = {
  // 验证支付数据
  validatePaymentData: (data) => {
    if (!data.amount || data.amount <= 0) {
      throw new Error('支付金额必须大于0')
    }
    if (!data.paymentMethod) {
      throw new Error('请选择支付方式')
    }
    if (!data.feeType) {
      throw new Error('费用类型不能为空')
    }
    if (!data.roomNumber) {
      throw new Error('房间号不能为空')
    }
  },

  // 格式化金额
  formatAmount: (amount) => {
    return parseFloat(amount).toFixed(2)
  },

  // 验证手机号
  validatePhone: (phone) => {
    const phoneRegex = /^1[3-9]\d{9}$/
    return phoneRegex.test(phone)
  },

  // 生成订单号
  generateOrderNo: () => {
    const timestamp = Date.now()
    const random = Math.floor(Math.random() * 1000)
    return `PAY${timestamp}${random.toString().padStart(3, '0')}`
  }
}

// 默认导出
export default {
  unifiedPaymentApi,
  paymentChannelApi,
  billApi,
  paymentRecordApi,
  invoiceApi,
  paymentSettingApi,
  feeTypeApi,
  paymentUtils
} 